#pragma once
#include <QDir>
#include <QPixmap>
#include <QDebug>

static const int MapSize = 2;

class PicturePuzzleTool {
private:
    PicturePuzzleTool() {}
    QList<QPixmap> m_pixmaps;

public:
    static PicturePuzzleTool& getInstance() {
        static PicturePuzzleTool instance;
        return instance;
    }

    void splitImage() {

        QString imagePath = QDir::currentPath() + "/1.jpg";
        QPixmap sourceImage(imagePath);
        //sourceImage = sourceImage.scaled(QSize(600,400));

        if (sourceImage.isNull()) {
            qWarning() << "Failed to load image:" << imagePath;
        }

        int blockWidth = sourceImage.width() / MapSize;
        int blockHeight = sourceImage.height() / MapSize;
        if (blockWidth <= 0 || blockHeight <= 0) {
            qWarning() << "Invalid image size for splitting";
        }

        for (int row = 0; row < MapSize; ++row) {
            for (int col = 0; col < MapSize; ++col) {
                QRect region(col * blockWidth,
                             row * blockHeight,
                             blockWidth,
                             blockHeight);
                QPixmap piece = sourceImage.copy(region);
                m_pixmaps.append(piece);
            }
        }
    }
    QPixmap* getPixmap(int num)
    {
        return &m_pixmaps[num];
    }

    QList<QPixmap>* getPixmaps()
    {
        return &m_pixmaps;
    }
};
